
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
public partial class frmPHIEU_XUAT_CT : Form
{
    public string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;
    string sMAHH = "";//FK_


    #region"    frmPHIEU_XUAT_CT"
    public frmPHIEU_XUAT_CT()
    {
        InitializeComponent();
    }
    #endregion

    #region"    frmPHIEU_XUAT_CT_Load"
    public void frmPHIEU_XUAT_CT_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;
        groupControl1.Text = groupControl1.Text + " - " + ma;
    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_PHIEU_XUAT_CT",
                           new string[] { "@flag", "@DOCID", "@MAHH" },
                           new object[] { 4, ma, sMAHH });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[50];
        object[] value = new object[50];
        int ipos = 0;
        param[ipos] = "@flag";
        value[ipos++] = 4;
        param[ipos] = "@DOCID";
        value[ipos++] = ma;

        grdPHIEU_XUAT_CT.AutoGenerateColumns = false;

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = othuvien.SP_SelectDataTable("sp_PHIEU_XUAT_CT", param, value);
        grdPHIEU_XUAT_CT.DataSource = tbMainDS;
        grdPHIEU_XUAT_CT.Refresh();

        //tbMainDS.TableName = "PHIEU_XUAT_CT";
        //tbMainDS.WriteXmlSchema(@"D:\Project\LPV-POS\LPV-POS\bin\Debug\report\ds_PHIEU_XUAT_CT.xsd");
    }
    #endregion
    #region"    showinfo"
    void showinfo(int i1Edt2New)
    {
        cboMAHH.Enabled = i1Edt2New == 2;
        this.showdata();
        if (tbMain.Rows.Count == 0)
        {

            if (cboMAHH.Items.Count > 0)
                cboMAHH.SelectedIndex = 0;

            txtDVT.Text = "";
            numSOLUONG.Value = 1;
            numGIABAN.Value = 0;
            numGIAMGIA_PHANTRAM.Value = 0;
            numGIAMGIA.Value = 0;
            //numVAT.Value = 0;
            numTHANHTIEN.Value = 0;
            
            txtGHICHU.Text = "";
            return;
        }

        cboMAHH.SelectedValue = tbMain.Rows[0]["MAHH"];

        txtDVT.Text = tbMain.Rows[0]["DVT"] + "";
        numSOLUONG.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["SOLUONG"]).Replace("0-", "-"));
        numGIABAN.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIABAN"]).Replace("0-", "-"));
        numGIAMGIA_PHANTRAM.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIAMGIA_PHANTRAM"]).Replace("0-", "-"));
        numGIAMGIA.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIAMGIA"]).Replace("0-", "-"));
        //numVAT.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["VAT"]).Replace("0-", "-"));
        numTHANHTIEN.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["THANHTIEN"]).Replace("0-", "-"));
        
        txtGHICHU.Text = tbMain.Rows[0]["GHICHU"] + "";

    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {

        thuvien othuvien = new thuvien();

        DataTable tbDM_HH = othuvien.SP_SelectDataTable("sp_PHIEU_XUAT_CT", new string[] { "@flag" }, new object[] { 5 });
        cboMAHH.DataSource = tbDM_HH;
        cboMAHH.SelectedIndex = 0;
        
        
    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        sMAHH = "";
        this.showinfo(2);
        this.setStatusButton(1);
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion
    
    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            sMAHH = "";//FK_
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            thuvien othuvien = new thuvien();//lớp truy xuat du lieu         
            _iReasult = othuvien.SP_Update("sp_PHIEU_XUAT_CT",
                 new string[] { "@flag", "@DOCID", "@MAHH", "@TEN_HH", "@DVT", "@SOLUONG", "@GIABAN", "@GIAMGIA_PHANTRAM", "@GIAMGIA", "@THANHTIEN", "@GIAVON", "@GHICHU" },
                 new object[] { 1, ma, cboMAHH.SelectedValue, cboMAHH.Text, txtDVT.Text, numSOLUONG.Text, numGIABAN.Text, numGIAMGIA_PHANTRAM.Text, numGIAMGIA.Text, numTHANHTIEN.Text, 0, txtGHICHU.Text });
        }
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    #region"    delete"
    int delete()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = othuvien.SP_Update("sp_PHIEU_XUAT_CT",
                                 new string[] { "@flag", "@DOCID", "@MAHH" },
                                 new object[] { 3, ma, sMAHH});
        return _iReasult;
    }

    #endregion
    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn xóa PHIẾU XUẤT này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa PHIẾU XUẤT thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                sMAHH = "";//FK_
                
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion
    #region"    sự kiên chọn trên lưới -- tblDanhSach"
    
    
    #region"    grdPHIEU_XUAT_CT_SelectionChanged"
    private void grdPHIEU_XUAT_CT_SelectionChanged(object sender, EventArgs e)
    {

        if (iloadform && grdPHIEU_XUAT_CT.CurrentRow != null)
        {
            if (grdPHIEU_XUAT_CT.CurrentRow.Index >= 0)
            {
                iloadform = false;
                sMAHH = "";
                string _iSTT = grdPHIEU_XUAT_CT.Rows[grdPHIEU_XUAT_CT.CurrentRow.Index].Cells[0].Value + "";
                DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'");
                if (_dr.Length > 0)
                {
                    sMAHH = _dr[0]["MAHH"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo(1);
                iloadform = true;
            }
        }
    }
    private void grdPHIEU_XUAT_CT_DoubleClick(object sender, EventArgs e)
    {
        this.grdPHIEU_XUAT_CT_SelectionChanged(null, null);
    }
    #endregion
    #endregion
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);
        
        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion
    
    #region"    grdPHIEU_XUAT_CT_DataError"
    private void grdPHIEU_XUAT_CT_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion
    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void cboMAHH_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            txtDVT.Text = "";
            try
            {
                object o1 = cboMAHH.SelectedItem;
                if (o1 != null)
                {
                    DataRow dr = ((DataRowView)o1).Row;
                    txtDVT.Text = dr["TEN_DVT"] + "";

                    decimal dgiaban = 0;
                    decimal.TryParse(dr["GIA1"] + "", out dgiaban);
                    numGIABAN.Value = dgiaban;
                }
            }
            catch (Exception EX)
            {
                string s = EX.Message;
            }
        }
    }
    #endregion

    private void calcEdit2_EditValueChanged(object sender, EventArgs e)
    {

    }

    private void numSOLUONG_EditValueChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            numGIAMGIA.Value = numSOLUONG.Value * numGIABAN.Value * numGIAMGIA_PHANTRAM.Value / 100;
            
            numTHANHTIEN.Value = numSOLUONG.Value * numGIABAN.Value - numGIAMGIA.Value;
        }
    }


}